let mor = require("../lib/mongo/mor");
let jv = require("../lib/sys/myHelper")

let roomService = {
    async getRoomInfo(salerId, userId) {
        let room = await mor.im.room.query().where({"users.id": {$all: [salerId, userId]}}).toEntity()
        if (!room) {

            let [saler, user] = await Promise.all([
                mor.sys.user.queryById(salerId).toEntity(),
                mor.sys.user.queryById(userId).toEntity()
            ])

            room = {users: [saler, user], name: saler.name + " - " + user.name}
            if (!await mor.im.room.insert(room)) {
                return null;
            }
        }
        return room;
    },
    async getRoomById(roomId) {
        return await mor.im.room.queryById(roomId).toEntity();
    },
    async updateRoom(room_id, roomInfo) {

        mor.im.room.update({_id: room_id}, roomInfo);
    },
    async message(content) {
        await mor.im.message.insert(content)
    }
};

Object.assign(exports, roomService)
